package com.jingxi.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingxi.common.pojo.EasyUIDataGridResult;
import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.common.util.HttpclientUtil;
import com.jingxi.mapper.TbContentMapper;
import com.jingxi.model.TbContent;
import com.jingxi.model.TbContentExample;
import com.jingxi.model.TbContentExample.Criteria;
import com.jingxi.model.TbItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    TbContentMapper tbContentMapper;
    @Override
    public EasyUIDataGridResult list(long categoryId, int page, int rows) {
        EasyUIDataGridResult easyUIDataGridResult=new EasyUIDataGridResult();
        TbContentExample tbContentExample=new TbContentExample();
//        Criteria criteria=tbContentExample.createCriteria();
//        criteria.andCategoryIdEqualTo(categoryId);
        PageHelper.startPage(1,10);
        List<TbContent>li=tbContentMapper.selectByExample(tbContentExample);
        PageInfo<TbContent> tol=new PageInfo<TbContent>(li);
        easyUIDataGridResult.setRows(li);
        easyUIDataGridResult.setTotal(tol.getTotal());
        return easyUIDataGridResult;
    }

    @Override
    public JingXiResult save(TbContent tbContent) {
        tbContent.setCreated(new Date());
        tbContent.setUpdated(new Date());
       int i= tbContentMapper.insert(tbContent);
       try {
           HttpclientUtil.get("http://localhost:8082/rest/cache/sync/content/"+tbContent.getCategoryId());
       }catch (Exception e){
           e.printStackTrace();
       }
       if(i>0){
           return JingXiResult.ok();
       }
        return JingXiResult.ok();
    }

    @Override
    public JingXiResult updata(TbContent tbContent) {
        tbContent.setUpdated(new Date());
        int i=tbContentMapper.updateByPrimaryKey(tbContent);
        try {
            HttpclientUtil.get("http://localhost:8082/rest/cache/sync/content/"+tbContent.getCategoryId());
        }catch (Exception e){
            e.printStackTrace();
        }
        return JingXiResult.ok();
    }

    @Override
    public JingXiResult delete(long id) {
        int i=tbContentMapper.deleteByPrimaryKey(id);
        return JingXiResult.ok();
    }
}
